package com.goods.business.service.imp;

import com.goods.business.mapper.StudentMapper;
import com.goods.business.pojo.Student;
import com.goods.business.service.StudentService;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.system.mapper.DepartmentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@ComponentScan(value = "com.goods.business.mappper")
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;

    @Override
    public Student selectById(Long id) {
        Student student = studentMapper.selectByPrimaryKey(id);
        return student;
    }

    @Override
    public void updatebyId(Long id, ProductCategoryVO map) {
//        {
//            "id": 24,
//                "name": "生活用品,电脑配件",
//                "remark": "生活用品,电脑配件",
//                "sort": 2,
//                "createTime": "2020-03-16T16:00:00.000+0000",
//                "modifiedTime": "2020-03-22T15:00:43.000+0000",
//                "pid": 0
//        }
//        Student student = new Student();
//        student.setId(id);
//        student.setModifiedTime(new Date());
//        student.setName(map.get("name").toString());
//        student.setRemark(map.get("remark").toString());
//        student.setSort((Integer)map.get("sort"));
//        String pid = map.get("pid").toString();
//        Long aLong = Long.valueOf(pid);
//        student.setPid(aLong);
//        String createTime = map.get("createTime").toString();
//        Date date = new Date();
//        //注意format的格式要与日期String的格式相匹配
//        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
//        DateFormat sdf3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//        try {
//            date = sdf3.parse(createTime);
//            System.out.println(date.toString());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        student.setCreateTime(date);
        Student student = new Student();
        BeanUtils.copyProperties(map, student);
        student.setModifiedTime(new Date());
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public Integer delById(Integer id) {
        Student student = studentMapper.selectByPrimaryKey(id);
        Long pid = student.getPid();
        System.out.println(pid);
        if (pid==0){
            return  1;
        }
        studentMapper.deleteByPrimaryKey(id);
//        departmentMapper.deleteByPrimaryKey(id);
        System.out.println("==================");
        return 0;
    }

    @Override
    public void add(Map map) {
        Student student = new Student();
//        {
//            "pid": 43,
//                "name": "王啊",
//                "remark": "tttttt",
//                "sort": 1
//        }
        student.setName(map.get("name").toString());
        student.setRemark(map.get("remark").toString());
        student.setSort((Integer)map.get("sort"));
        String pid = map.get("pid").toString();
        Long aLong = Long.valueOf(pid);
        student.setPid(aLong);
        student.setCreateTime(new Date());
        studentMapper.insert(student);
    }

    @Override
    public List<ProductCategoryTreeNodeVO> toClass() {
        List<Student> students = studentMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        for (Student student : students) {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO1 = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(student, productCategoryTreeNodeVO1);
            productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO1);
        }
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS1 = CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);
        return productCategoryTreeNodeVOS1;
    }

    @Override
    public List<ProductCategoryTreeNodeVO> selepage(Integer pageNum, Integer pageSize) {
        List<Student> students = studentMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        for (Student student : students) {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO1 = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(student, productCategoryTreeNodeVO1);
            productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO1);
        }
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(build, pageSize, pageNum);
        return page;
    }

}

